﻿Imports System.Net.Mail
Imports System.IO
Imports System.IO.Packaging
Imports System.Net.Mime

Namespace packagers.impl
    Public Class EmailPackager
        Inherits BasePackager
        Implements IPackager

        Private Const PROPERTIES_PREFIX As String = "email"

        Public Sub package(req As domain.Request) Implements IPackager.package
            Dim SmtpServer As New SmtpClient()

            Dim confId As String = req.Configuration_Id
            If (req.Configuration_Id = "") Then
                confId = "default"
            End If

            Dim confPrefix As String = PROPERTIES_PREFIX & "." & confId & "."

            ' smtp configuration from web.config
            Dim userId As String = readProperty(confPrefix & "userName")
            Dim password As String = readProperty(confPrefix & "password")
            Dim hostName As String = readProperty(confPrefix & "Host")
            Dim portNumber As Long = Long.Parse(readProperty(confPrefix & "Port"))
            Dim enableSSL As Boolean = Boolean.Parse(readProperty(confPrefix & "SSL"))

            SmtpServer.Credentials = New Net.NetworkCredential(userId, password)
            SmtpServer.Host = hostName
            SmtpServer.Port = portNumber
            SmtpServer.EnableSsl = enableSSL

            ' Build and send email
            Dim mail As MailMessage = BuildMail(readProperty(confPrefix & "destination"), req)

            'TODO: Pack attachment in email
            Dim bt64 As Byte() = System.Convert.FromBase64String(req.File)
            Dim fileName As String = req.File_Name & "." & req.File_Type
            Dim att As Attachment
            Dim ms As New MemoryStream()

            Dim ct As New ContentType()
            ct.MediaType = MediaTypeNames.Application.Octet

            If (req.Zippable) Then
                'Zip and attach
                Dim zip As Package = ZipPackage.Open(ms, IO.FileMode.Create)
                ' zipped in super class (BasePackager)
                zipFile(zip, bt64, fileName)
                zip.Close()
                ' Position @ Origin
                ms.Seek(0, SeekOrigin.Begin)

                ct.MediaType = MediaTypeNames.Application.Zip
                ct.Name = req.File_Name & ".zip"

                att = New Attachment(ms, ct)
            Else
                'Attach
                ms.Write(bt64, 0, bt64.Length)
                ' Position @ Origin
                ms.Seek(0, SeekOrigin.Begin)

                ct.Name = fileName

                att = New Attachment(ms, ct)
            End If

            mail.Attachments.Add(att)

            SmtpServer.Send(mail)
            ms.Close()
            ms.Dispose()

        End Sub

        Protected Function BuildMail(ByVal destination As String, ByRef request As domain.Request) As MailMessage
            Dim mail As New MailMessage()

            Dim mailSubject As String = "Packaged Email from service"
            Dim mailBody As String = "This is an automatic email generated by dtorres.packager web service, please do not respond to the email."
            Dim mailFrom As String = "diego.torres.fuerte@gmail.com"

            mail = New MailMessage()
            mail.From = New MailAddress(mailFrom)
            mail.To.Add(destination)

            mail.Subject = mailSubject
            mail.Body = mailBody

            Return mail
        End Function

    End Class
End Namespace
